﻿namespace TfsExtensions.Build.Activities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Activities;
    using Microsoft.TeamFoundation.Build.Workflow.Tracking;
    using Microsoft.TeamFoundation.Build.Workflow.Activities;
    using Microsoft.TeamFoundation.Build.Client;
    using System.Globalization;

    public static class LogHelper
    {
        public static void LogError(CodeActivityContext context, string message,  params object[] parameters)
        {
            Console.WriteLine(String.Format(message, parameters));
            context.Track(
                 new BuildInformationRecord<BuildMessage>()
                 {
                     Value = new BuildMessage()
                     {
                         Importance = BuildMessageImportance.High,
                         Message = String.Format(message, parameters)
                     }
                 });
        }

        public static void LogMessage(CodeActivityContext context, string message, params object[] parameters)
        {
            LogMessage(context, BuildMessageImportance.High, message, parameters );
        }

        public static void LogMessage(CodeActivityContext context, BuildMessageImportance importance, string message, params object[] parameters)
        {
            var messageToLog = message;
            if (parameters != null && parameters.Length > 0)
            {
                messageToLog = String.Format(CultureInfo.InvariantCulture, message, parameters);
            }

            Console.WriteLine(messageToLog);
            context.Track(
                 new BuildInformationRecord<BuildMessage>()
                 {
                     Value = new BuildMessage()
                     {
                         Importance = BuildMessageImportance.High,
                         Message = messageToLog
                     }
                 });
        }

        public static void LogWarning(CodeActivityContext context, string message, params object[] parameters)
        {
            Console.WriteLine(String.Format(message, parameters));
            context.Track(
                 new BuildInformationRecord<BuildWarning>()
                 {
                     Value = new BuildWarning()
                     {
                         Message = String.Format(CultureInfo.InvariantCulture, message, parameters)
                     }
                 });
        }
    }
}
